package org.fansea;

/**
 * @Projectname: HandTornQuestions
 * @Filename: LinkedQueue
 * @Author: FANSEA
 * @Date:2024/10/27 22:27
 */
public class LinkedQueue<T> {

    private Node<T> head;
    private Node<T> tail;
    private int size;
    public LinkedQueue() {
        head = null;
        tail = null;
        size = 0;
    }

    // 入队
    public void enqueue(T t) {
        Node<T> node = new Node<>(t);
        if (size == 0){
            tail = node;
            head = node;
            size++;
            return;
        }
        size++;
        head.pre = node;
        node.next = head;
        head = node;
    }

    // 出队
    public T dequeue() {
        if (tail == null){
            return null;
        }
        size--;
        T val = tail.val;
        tail = tail.pre;
        return val;
    }
    private static class Node<T> {
        T val;
        Node<T> next;
        Node<T> pre;
        public Node(T val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        LinkedQueue<Integer> queue = new LinkedQueue<>();
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
    }


}
